﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using Newtonsoft.Json;
using Telerik.Web.UI;

namespace RN.ERP.Web.EnterpriseChange
{
    public partial class ChangeInfo : System.Web.UI.Page
    {
        public string CorpUserGuid 
        {
            get
            {
                if (string.IsNullOrEmpty(Request["corpUserType"]))
                {
                    SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
                    return user.CorpUserGuid;
                }
                return Request["corpUserType"];

            }
        }
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(CorpUserGuid))
                {
                    BindData();
                }
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            if (e.Argument == "Rebind")
            {
                ListCorpChange.MasterTableView.SortExpressions.Clear();
                ListCorpChange.MasterTableView.GroupByExpressions.Clear();
                ListCorpChange.Rebind();
            }

        }

        protected void ListCorpChange_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            int corpChangeCount = 0;
            SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            var strCorpChangeList = basicServiceClient.GetChanges(out corpChangeCount, "corp", user.CorpUserGuid, "", "", ListCorpChange.CurrentPageIndex + 1, ListCorpChange.PageSize, user.CurrentArea);
            var corpChangeList = JsonConvert.DeserializeObject<List<V_ChangeInfo>>(strCorpChangeList);
            ListCorpChange.VirtualItemCount = corpChangeCount;
            ListCorpChange.DataSource = corpChangeList;
        }

        private void BindData()
        {
            var corp = GetCompanyinfo(CorpUserGuid);
            if (corp != null)
            {
                ltrCorpName.Text = corp.CorpName;
                hdnCorpUserGuid.Value = CorpUserGuid;
                ucEnterpriseChange.ObjectId = CorpUserGuid;
                ucEnterpriseChange.ObjectName = corp.CorpName;
            }
        }

        private Tb_Corp_CompanyInfo GetCompanyinfo(string corpUserGuid)
        {
            QueryCompanyInfoCondition queryCompanyInfoCondition =
               new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(corpUserGuid), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), user.CurrentArea));
            return corpInfo;
        }

        public string GetFlowCodeTitle(object oflowcode)
        {
            if (oflowcode == null)
            {
                return string.Empty;
            }
            string flowcode = oflowcode.ToString().Trim();
            if (flowcode.StartsWith("1"))
            {
                return "审核通过";
            }
            else if (flowcode.StartsWith("0,019,021"))
            {
                return "主管退回";
            }
            else if (flowcode.IndexOf(",022") > 0)
            {
                return "再次上报";
            }
            else if (flowcode.StartsWith("0,019"))
            {
                return "已上报";
            }
            else if (flowcode.Trim().Equals("0"))
            {
                return "未上报";
            }
            else
            {
                return string.Empty;
            }
        }
    }
}